import asyncio

from loguru import logger

from com.arcfox.base.base_spider import ExecuteType
from com.arcfox.school.processor.yzy.yzy_school_list_processor import YZYSchoolListProcessor
from com.arcfox.school.spider.yzy.yzy_base_spider import YZYBaseSpider
from com.arcfox.util import async_request as requests


class YZYSchoolListSpider(YZYBaseSpider):
    '''
    优志愿学校列表抓取, 用列表数据抓取学校相关的图片, 以及学校编码更新到tb_university_info
    '''

    def __init__(self):
        super().__init__()
        self.url = "https://uwf7de983aad7a717eb.youzy.cn/youzy.dms.basiclib.api.college.query"
        self.start_page = 1
        self.processor = YZYSchoolListProcessor()

    def get_params(self, page_num):
        return '{"keyword":"","provinceNames":[],"natureTypes":[],"eduLevel":"","categories":[],"features":[],' \
               '"pageIndex":' + str(page_num) + ',"pageSize":20,"sort":11}'

    def get_headers(self, params):
        return {
            "Host": "uwf7de983aad7a717eb.youzy.cn",
            "u-sign": self.get_u_sgin(params),
            "Content-Type": "application/json",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
            "Referer": "https://pv4y-pc.youzy.cn/",
        }

    async def _pull_task(self):
        return None, ExecuteType.ONCE

    async def _crawl_by_task(self, task):
        params = self.get_params(self.start_page)
        resp = await requests.post_with_session(url=self.url, data=params, headers=self.get_headers(params))
        logger.info("crawl school list page: {},{}", resp.code,resp.response)
        if resp.code == 200:
            result_json = resp.json()
            await self.processor.parse_and_save_data(result_json)
            total_count = result_json['result']["totalCount"]
            total_pages = total_count / 20 if total_count % 20 == 0 else total_count / 20 + 1
            for i in range(2, int(total_pages) + 1):
                params_item = self.get_params(i)
                resp_item = await requests.post_with_session(url=self.url, data=params_item,
                                                             headers=self.get_headers(params_item))
                if resp_item.code == 200:
                    await self.processor.parse_and_save_data(resp_item.json())
                    # await random_sleep(0.2)


if __name__ == "__main__":
    asyncio.run(YZYSchoolListSpider().start_crawl())
